let categoryid=location.href.split("=")[1]
let main=document.getElementById("main")

let num=1


function getPageList(categoryid=0, num) {
      const getPage = () => ajax({
        url: "http://chst.vip:1234/api/getproductlist",
        data: { categoryid: categoryid, pageid: num },
    })
    getPage()
        .then(res => {
            console.log(res);
            let html=""
            res.result.forEach(res => {
                html +=`
                <div id="mainList">
            <div id="img">${res.productImg}</div>
            <div id="text">
                <p>${res.productName}</p>
                <p>${res.productPrice}</p>
                <p>${res.productQuote}  ${res.productCom}</p>
                
            </div>
            </div>



                `

            });
            main.innerHTML=html
            // console.log(res);
            console.log(res);
            
           
        })
   
}
getPageList(categoryid,num)

//动态创建下拉页
let page=document.getElementById("page")


const getCart = (categoryid=0,num) => ajax({ //
    url: "http://chst.vip:1234/api/getproductlist",
    data: { categoryid: categoryid, pageid: num },
    
})

getCart(categoryid,num)
    .then(res => {
        // getPageList(categoryid,3)   //可以更新页面
        console.log(res);
        let pagenum=Math.ceil( res.totalCount/res.pagesize)
        // console.log(pagenum);
        var html1 = ""
       for(var i=1;i<=pagenum;i++) { 
           
            html1 +=`
            <option value="">${i} /${pagenum}</option>
            `
        }
        page.innerHTML=html1
       
        btnLeft.disabled=true
        // num在最上面已给初始值0
        btnright.onclick = () => {  //下一页点击事件
            btnLeft.disabled=false
            main.innerHTML = ''
            num++
            
            getPageList(categoryid,num)
            
            console.log(num);
            if(num===pagenum){
                btnright.disabled=true  //按钮变灰
            }
            if (num <pagenum+1) {
            //    console.log(num);
                page[num-1].selected=true   //中间页码跟随当前属性被选中
              
            } 
            
        }

        btnLeft.onclick = () => {  //上一页点击事件
       
            main.innerHTML = ''
            num--
            // console.log(num);
            btnright.disabled=false
            if(num===1){
                btnLeft.disabled=true
            }
            if (num >=1) {
                getPageList(categoryid, num)
               
                page[num-1].selected=true
            } 
        }


        page.onchange = () => {   //页码选项事件

            btnLeft.disabled=false
            num =page.selectedIndex+1
            if(num===0){
                btnLeft.disabled=true
            }
           if(num===pagenum){
               btnright.disabled=true
           }else{
            btnright.disabled=false
           }
           
            console.log(num);
            
            main.innerHTML = ''
            getPageList(categoryid, num)
           }

    })
